<?php

class UserIdentity extends CUserIdentity {

    private $_id;

    public function getId() {
        return $this->_id;
    }

    public function authenticate() {
        $record = User::model()->findByAttributes(array(User::FIELD_LOGIN => $this->username));

        if ($record === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } elseif ($record->password !== md5(md5($this->password) . md5(User::IDENTITY_SALT))) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id = $record->id;
            $this->setState('name', $record->name);
            $this->errorCode = self::ERROR_NONE;
        }

        return !$this->errorCode;
    }
}
